package com.eqxiu.ieditor.weixin.service;

import com.eqxiu.ieditor.weixin.api.WeixinMenuServiceApi;
import com.eqxiu.ieditor.weixin.dao.BottomMenuMapper;
import com.eqxiu.ieditor.weixin.dto.BottomMenuAttrDTO;
import com.eqxiu.ieditor.weixin.dto.BottomMenuReqDTO;
import com.eqxiu.ieditor.weixin.model.BottomMenu;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class WeixinMenuServiceImpl implements WeixinMenuServiceApi {

	private Logger logger = LoggerFactory.getLogger(getClass().getName());

	@Resource
	private BottomMenuMapper bottomMenuMapper;

	@Override
	public String findWeixinMenuList() {

		Map<String, List<BottomMenuReqDTO>> rtnMap = new HashMap<>(16);
		List button = new ArrayList<>(16);
		rtnMap.put("button", button);
		BottomMenu bottomMenu = new BottomMenu();
		bottomMenu.setStatus((byte)1);
		List<BottomMenu> menuList = bottomMenuMapper.findMenuList(bottomMenu);
		List<BottomMenu> firstMenuList = menuList.stream()
				.filter(item -> item.getUpperId() == 0)
				.sorted(Comparator.comparing(BottomMenu::getSort))
				.collect(Collectors.toList());
		logger.info("一级菜单列表为：{}", firstMenuList);

		List<BottomMenu> allSecondMenuList = menuList.stream()
				.filter(item -> item.getUpperId() != 0)
				.collect(Collectors.toList());
		logger.info("所有二级菜单列表为：{}", firstMenuList);

		firstMenuList.forEach(item1 -> {

			List<BottomMenu> secondMenuList  = allSecondMenuList.stream().filter(item2 -> item2.getUpperId().equals(item1.getId()))
					.sorted(Comparator.comparing(BottomMenu::getSort))
					.collect(Collectors.toList());
			logger.info("一级id为:{}的二级菜单列表为:{}", item1.getId(), secondMenuList);
			if (secondMenuList.isEmpty()) {
				BottomMenuAttrDTO temp = new BottomMenuAttrDTO();
				temp.setAppid(item1.getAppid());
				temp.setName(item1.getMenuName());
				temp.setPagepath(item1.getPagePath());
				temp.setType(item1.getType()==1?"view":"miniprogram");
				temp.setUrl(item1.getUrl());
				button.add(temp);
				return;
			}
			BottomMenuReqDTO temp1 = new BottomMenuReqDTO();
			temp1.setName(item1.getMenuName());
			List<BottomMenuAttrDTO> sub_button = new ArrayList<>(16);
			temp1.setSub_button(sub_button);
			secondMenuList.forEach(item3 -> {
				BottomMenuAttrDTO temp2 = new BottomMenuAttrDTO();
				temp2.setAppid(item3.getAppid());
				temp2.setName(item3.getMenuName());
				temp2.setPagepath(item3.getPagePath());
				temp2.setType(item3.getType()==1?"view":"miniprogram");
				temp2.setUrl(item3.getUrl());
				sub_button.add(temp2);
			});
			button.add(temp1);
			System.out.println("------------------------------------------------------------");
            String s = new Gson().toJson(button);
            System.out.println(s);
		});
		logger.info("======================================================");
		String s = new GsonBuilder().disableHtmlEscaping().create().toJson(rtnMap);
		System.out.println(s);

		return s;
	}
}
